#!/bin/bash

# 镜像仓库路径
harbor=192.168.2.30
project=mango-10d

# 进入当前脚本所在路径
cd $(cd `dirname $0`;pwd)

# 获取模块信息
module=$(awk '/<artifactId>[^<]+<\/artifactId>/{gsub(/<artifactId>|<\/artifactId>/,"",$1);print $1;exit;}' ./pom.xml)
version=$(awk '/<version>[^<]+<\/version>/{gsub(/<version>|<\/version>/,"",$1);print $1;exit;}' ./pom.xml)
image_url="${harbor}/${project}/${module}:${version}"

# 备份
mkdir -p /deploy /deploy_backup
if [ -f "/deploy/${module}.yml" ]; then
    mv /deploy/${module}.yml /deploy_backup/$(date '+%Y%m%d-%H%M%S')-${module}.yml
fi

# 替换镜像版本
sed -i "s#image: .*#image: ${image_url}#" ./${module}.yml
cp ./${module}.yml /deploy/${module}.yml

# 启动
echo "-------------- 启动 /deploy/${module}.yml --------------"
docker-compose -f /deploy/docker-compose.yml -p mango up -d ${module}
(sleep 60; kill -9 $(pidof docker-compose)) &
docker-compose -f /deploy/docker-compose.yml -p mango logs -f ${module}
echo "------------------- 启动日志打印完成 -------------------"